#pragma once

using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::Collections::Generic;

namespace StereoCorrespond {

	/// <summary>
	/// Summary for PictureViewer
	/// </summary>
	public ref class PictureViewer : public System::Windows::Forms::UserControl
	{
	private:
		Image^ image;

		float zoomFactor;
		Point CenterPixel;

		System::Void zoomUpdated(float old);
		System::Void clientSizeUpdated();
		System::Collections::Generic::List<KeyValuePair<Point, float> > Features;
		int Current;
		int fsize;

	public:

		void clearfeatures();
		void addfeature(int x, int y, float r);
		bool next();
		bool prev();

		property float ZoomFactor
		{
			float get()
			{
				return zoomFactor;
			}

			System::Void set(float factor)
			{
				float old = zoomFactor;;
				zoomFactor = factor;
				zoomUpdated(old);
			}
		}

		property Image^ Image
		{
			System::Drawing::Image^ get()
			{
				return image;
			}

			System::Void set(System::Drawing::Image^ img)
			{
				if(img)
				{
					zoomInButton->Enabled = true;
					zoomOutButton->Enabled = true;
					saveToolStripMenuItem->Enabled = true;
				}
				else
				{
					zoomInButton->Enabled = false;
					zoomOutButton->Enabled = false;
					saveToolStripMenuItem->Enabled = false;
					zoomLabel->Text = nullptr;
					zoomFactor = 1.0f;
				}

				image = img;
				DrawPanel->Invalidate();
			}
		}

		System::Void imageUpdated();

		PictureViewer(void)
		{
			InitializeComponent();
			
			zoomInButton->Enabled = false;
			zoomOutButton->Enabled = false;
			saveToolStripMenuItem->Enabled = false;
			zoomLabel->Text = nullptr;
			zoomFactor = 1.0f;

			DrawPanel->Size = scrollPanel->ClientSize;
			Current = -1;
			fsize = 0;
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~PictureViewer()
		{
			if (components)
			{
				delete components;
			}
		}

	private: System::Windows::Forms::Panel^  DrawPanel;
	private: System::Windows::Forms::ContextMenuStrip^  contextMenuStrip;
	private: System::Windows::Forms::ToolStripMenuItem^  saveToolStripMenuItem;
	private: System::Windows::Forms::Button^  zoomInButton;
	private: System::Windows::Forms::Button^  zoomOutButton;
	private: System::ComponentModel::IContainer^  components;
	private: System::Windows::Forms::Label^  zoomLabel;
	private: System::Windows::Forms::Panel^  scrollPanel;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(PictureViewer::typeid));
			this->DrawPanel = (gcnew System::Windows::Forms::Panel());
			this->contextMenuStrip = (gcnew System::Windows::Forms::ContextMenuStrip(this->components));
			this->saveToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->zoomInButton = (gcnew System::Windows::Forms::Button());
			this->zoomOutButton = (gcnew System::Windows::Forms::Button());
			this->zoomLabel = (gcnew System::Windows::Forms::Label());
			this->scrollPanel = (gcnew System::Windows::Forms::Panel());
			this->saveFileDialog = (gcnew System::Windows::Forms::SaveFileDialog());
			this->contextMenuStrip->SuspendLayout();
			this->scrollPanel->SuspendLayout();
			this->SuspendLayout();
			// 
			// DrawPanel
			// 
			this->DrawPanel->Location = System::Drawing::Point(0, 0);
			this->DrawPanel->Name = L"DrawPanel";
			this->DrawPanel->Size = System::Drawing::Size(50, 50);
			this->DrawPanel->TabIndex = 0;
			this->DrawPanel->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &PictureViewer::DrawPanel_Paint);
			this->DrawPanel->Resize += gcnew System::EventHandler(this, &PictureViewer::DrawPanel_Resize);
			// 
			// contextMenuStrip
			// 
			this->contextMenuStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->saveToolStripMenuItem});
			this->contextMenuStrip->Name = L"contextMenuStrip";
			this->contextMenuStrip->Size = System::Drawing::Size(99, 26);
			// 
			// saveToolStripMenuItem
			// 
			this->saveToolStripMenuItem->Name = L"saveToolStripMenuItem";
			this->saveToolStripMenuItem->Size = System::Drawing::Size(98, 22);
			this->saveToolStripMenuItem->Text = L"Save";
			this->saveToolStripMenuItem->Click += gcnew System::EventHandler(this, &PictureViewer::saveToolStripMenuItem_Click);
			// 
			// zoomInButton
			// 
			this->zoomInButton->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"zoomInButton.BackgroundImage")));
			this->zoomInButton->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
			this->zoomInButton->Location = System::Drawing::Point(3, 3);
			this->zoomInButton->Name = L"zoomInButton";
			this->zoomInButton->Size = System::Drawing::Size(23, 23);
			this->zoomInButton->TabIndex = 0;
			this->zoomInButton->UseVisualStyleBackColor = true;
			this->zoomInButton->Click += gcnew System::EventHandler(this, &PictureViewer::zoomInButton_Click);
			// 
			// zoomOutButton
			// 
			this->zoomOutButton->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"zoomOutButton.BackgroundImage")));
			this->zoomOutButton->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
			this->zoomOutButton->Location = System::Drawing::Point(32, 3);
			this->zoomOutButton->Name = L"zoomOutButton";
			this->zoomOutButton->Size = System::Drawing::Size(23, 23);
			this->zoomOutButton->TabIndex = 1;
			this->zoomOutButton->UseVisualStyleBackColor = true;
			this->zoomOutButton->Click += gcnew System::EventHandler(this, &PictureViewer::zoomOutButton_Click);
			// 
			// zoomLabel
			// 
			this->zoomLabel->AutoSize = true;
			this->zoomLabel->Location = System::Drawing::Point(61, 8);
			this->zoomLabel->Name = L"zoomLabel";
			this->zoomLabel->Size = System::Drawing::Size(0, 13);
			this->zoomLabel->TabIndex = 2;
			// 
			// scrollPanel
			// 
			this->scrollPanel->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->scrollPanel->AutoScroll = true;
			this->scrollPanel->ContextMenuStrip = this->contextMenuStrip;
			this->scrollPanel->Controls->Add(this->DrawPanel);
			this->scrollPanel->Location = System::Drawing::Point(4, 33);
			this->scrollPanel->Name = L"scrollPanel";
			this->scrollPanel->Size = System::Drawing::Size(193, 164);
			this->scrollPanel->TabIndex = 3;
			this->scrollPanel->Scroll += gcnew System::Windows::Forms::ScrollEventHandler(this, &PictureViewer::scrollPanel_Scroll);
			// 
			// saveFileDialog
			// 
			this->saveFileDialog->DefaultExt = L"bmp";
			this->saveFileDialog->Filter = L"Bitmap (*.bmp)|*.bmp|JPEG (*.jpg,*jpeg)|*.jpg;*.jpeg|TIF (*.tif)|*.tif|All files " 
				L"(*.*)|*.*";
			this->saveFileDialog->Title = L"Save image";
			// 
			// PictureViewer
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->Controls->Add(this->scrollPanel);
			this->Controls->Add(this->zoomLabel);
			this->Controls->Add(this->zoomInButton);
			this->Controls->Add(this->zoomOutButton);
			this->Name = L"PictureViewer";
			this->Size = System::Drawing::Size(200, 200);
			this->contextMenuStrip->ResumeLayout(false);
			this->Invalidated += gcnew System::Windows::Forms::InvalidateEventHandler(this, &PictureViewer::main_invalidated);
			this->scrollPanel->ResumeLayout(false);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		private: System::Void saveToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e);
		private: System::Void DrawPanel_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e);
		private: System::Void DrawPanel_Resize(System::Object^  sender, System::EventArgs^  e);
		private: System::Void zoomInButton_Click(System::Object^  sender, System::EventArgs^  e);
		private: System::Void zoomOutButton_Click(System::Object^  sender, System::EventArgs^  e);
		private: System::Void scrollPanel_Scroll(System::Object^  sender, System::Windows::Forms::ScrollEventArgs^  e);
		private: System::Void main_invalidated(System::Object^ sender, System::Windows::Forms::InvalidateEventArgs^  e) {
					 DrawPanel->Invalidate();
			}
	};
}
